package com.song.cmsservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.song.cmsservice.entity.CrmBanner;
import com.song.cmsservice.service.CrmBannerService;
import com.song.commonutils.ResultObj;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author song
 * @since 2020-04-03
 */
@RestController
@RequestMapping("/eduservice/banneradmin")
@Api(description = "后台轮播图")
public class BannerAdminController {

    @Autowired
    private CrmBannerService crmBannerService;

    /**
     * 分页查询
     *
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "轮播图")
    @GetMapping("/{page}/{limit}")
    public ResultObj pageBanner(@PathVariable long page, @PathVariable long limit) {
        Page<CrmBanner> iPage = new Page<>();
        crmBannerService.page(iPage, null);
        return ResultObj.ok().data("total", iPage.getTotal()).data("data", iPage.getRecords());
    }


    /**
     * 添加轮播图
     *
     * @param crmBanner
     * @return
     */
    @ApiOperation(value = "添加轮播图")
    @PostMapping("/")
    public ResultObj addBanner(@RequestBody CrmBanner crmBanner) {
        boolean save = crmBannerService.save(crmBanner);
        if (save) {
            return ResultObj.ADD_SUCCESS;
        } else {
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 修改轮播图
     *
     * @param crmBanner
     * @return
     */
    @ApiOperation(value = "修改轮播图")
    @PutMapping("/")
    public ResultObj updateBanner(@RequestBody CrmBanner crmBanner) {
        boolean b = crmBannerService.updateById(crmBanner);
        if (b) {
            return ResultObj.UPDATE_SUCCESS;
        } else {
            return ResultObj.UPDATE_ERROR;
        }

    }

    /**
     * 删除轮播图
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除轮播图")
    @DeleteMapping("/{id}")
    public ResultObj deleteBanner(@PathVariable String id) {
        boolean b = crmBannerService.removeById(id);
        if (b) {
            return ResultObj.DELETE_SUCCESS;
        } else {
            return ResultObj.DELETE_ERROR;
        }

    }

    /**
     * 按照Id查询轮播图
     * @param id
     * @return
     */
    @ApiOperation(value = "按照Id查询轮播图")
    @GetMapping("/findById/{id}")
    public ResultObj findById(@PathVariable String id) {
        CrmBanner byId = crmBannerService.getById(id);
        return ResultObj.ok().data("data",byId);
    }
}

